package com.example.springdemo.Controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.example.springdemo.entity.Goods;
import com.example.springdemo.service.IGoodsService;
import com.example.springdemo.utils.PageResult;
import com.example.springdemo.utils.PassToken;
import com.example.springdemo.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * goodsController
 * 
 * @author ruoyi
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/system/goods")
public class GoodsController
{
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询goods列表
     */
   @PassToken
    @GetMapping("/list")
    public ResponseResult list(Goods goods, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize)
    {
        // startPage();
        PageResult pageResult =goodsService.selectGoodsList(goods,pageNum,pageSize);

        return ResponseResult.createBySuccess(200,pageResult,"查询成功");
    }
    @PassToken
    @GetMapping("/frontlist")
    public ResponseResult list1(Goods goods) {
        System.out.println(goods.getCid());
        List<Goods> list = goodsService.selectGoodsListNoPage(goods);
        return ResponseResult.createBySuccess(200,list,"获取成功");
    }
    @PassToken
    @GetMapping("/list2")
    public ResponseResult list2(Goods goods){
       List<Goods> list = goodsService.selectGoodsCommnets(goods);
       return ResponseResult.createBySuccess(200,list,"数据成功");
    }

    /**
     * 获取goods详细信息
     */
    @PassToken
    @GetMapping(value = "/{gid}")
    public ResponseResult getInfo(@PathVariable("gid") Long gid)
    {
        Goods goods = goodsService.selectGoodsByGid(gid);
        return ResponseResult.createBySuccess(200,goods,"查询成功");
    }

    /**
     * 新增goods
     */
    @PassToken
    @PostMapping
    public ResponseResult add(@RequestBody Goods goods)
    {
        int i = goodsService.insertGoods(goods);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "添加成功");
        } else {
            return ResponseResult.createByFail(200, "添加失败");
        }
    }

    /**
     * 修改goods
     */
    @PassToken
    @PutMapping
    public ResponseResult edit(@RequestBody Goods goods)
    {
        int i = goodsService.updateGoods(goods);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "修改成功");
        } else {
            return ResponseResult.createByFail(200, "修改失败");
        }
    }

    /**
     * 删除goods
     */
    @PassToken
	@DeleteMapping("/{gids}")
    public ResponseResult remove(@PathVariable Long[] gids)
    {
        int i = goodsService.deleteGoodsByGids(gids);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "删除成功");
        } else {
            return ResponseResult.createByFail(200, "删除失败");
        }
    }
}
